<?php

class App_Model_DbTable_Links extends Ext_Db_Table  
{
    protected $_name = 'links';
    
    private static $_linksRow = array();
    
    public function getRowById($id)
    {
        if(!$id)
        {
            return "";
        }
        
        if(isset(self::$_linksRow['id'][$id]))
        {
            return self::$_linksRow['id'][$id];
        }
        
        $db = $this->getAdapter();
        $safe = $db->quoteInto(" id = ? ", $id);
        $row = $this->fetchRow($safe);
        if (!$row) {
            return "";
        }
        $linksRow = $row->toArray();

        self::$_linksRow['id'][$id] = $linksRow;

        return $linksRow;
    }
    
    public function getMaxSequence()
    {
        $db = $this->getAdapter();
        $select = $db->select();
        $select
            ->from($this->_name, "MAX(sequence)")
            ;
        $sequence = $db->fetchOne($select);
        return $sequence;
    }

    public function del($id)
    {
        $db = $this->getAdapter();
        $safe = $db->quoteInto(" id = ? ", $id);
        $rows_affected = $this->delete($safe);
        if($rows_affected){
            $this->_unsetRow($id);
        }
        return $rows_affected;
    }
    
    public function getAll()
    {
        $db = $this->getAdapter();
        $select = $db->select();
        $select
            ->from($this->_name, "*")
            ->order('sequence ASC')
            ;
        
        $list = $db->fetchAll($select);
        return $list;
    }
    
    public function getCount($count = 20)
    {
        $db = $this->getAdapter();
        $select = $db->select();
        $select
            ->from($this->_name, "*")
            ->order('sequence ASC')
            ->limit($count)
            ;
        
        $list = $db->fetchAll($select);
        return $list;
    }
    
    public function addNew($inputArray)
    {
        $row = array (
            'siteName' => $inputArray['siteName'],
            'siteLink' => $inputArray['siteLink'],
            'sequence' => $inputArray['sequence'],
        );

        try {
            $rows_affected = $this->insert($row);
            $last_insert_id = $this->getAdapter()->lastInsertId();
        }catch (Exception $e){
            trigger_error($e->getMessage(), E_USER_WARNING);
        }
        return $last_insert_id;
    }
    
    public function addOld($inputArray, $id)
    {
        $id = intval($id);
        
        $sql_data_array = array();
        if(array_key_exists('siteName', $inputArray)) $sql_data_array['siteName'] = $inputArray['siteName'];
        if(array_key_exists('siteLink', $inputArray)) $sql_data_array['siteLink'] = $inputArray['siteLink'];
        if(array_key_exists('sequence', $inputArray)) $sql_data_array['sequence'] = $inputArray['sequence'];
        if(!$sql_data_array){
            return "";
        }
        
        try {
            $db = $this->getAdapter();
            $where = $db->quoteInto(" id = ? ", $id);
            $rows_affected = $this->update($sql_data_array, $where);
            if($rows_affected){
                $this->_unsetRow($id);
            }
        }catch (Exception $e){
            trigger_error($e->getMessage(), E_USER_WARNING);
        }
        
        return "";
    }
    
    private function _unsetRow($id)
    {
        if(!$id)
        {
            return "";
        }
        
        $linksRow = $this->getRowById($id);
        if(!$linksRow)
        {
            return "";
        }
        unset(self::$_linksRow['id'][$id]);
    }
}  

